# 实现井子琪
class checkerboard:
    board = [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]

    def __init__(self):
        print(f"绘制一个边长为3的棋盘")

    def fill(self, str, x, y):
        if x < 3 and y < 3:
            self.board[x][y] = str
            if self.ifwin(str):
                print(f"{str}胜利")
                print(self.board)
        else:
            print("此子无效，请重新下子。。。")

    # 有三种情况：横竖对角线
    def ifwin(self, str):
        return self.linewin(str) or self.rowwin(str) or self.diagonalwin(str)

    # 只判断行
    def linewin(self, str):
        flag = True
        for i in range(0, 3):
            for j in range(0, 3):
                if j > 0:
                    if self.board[i][j] != self.board[i][j - 1]:
                        flag = False
                        break
        return flag

    # 只判断列
    def rowwin(self, str):
        flag = True
        for i in range(0, 3):
            for j in range(0, 3):
                if j > 0:
                    if (self.board[j][i] != self.board[j - 1][i]):
                        flag = False
                        break
        return flag

    # 只判断对角线
    def diagonalwin(self, str):
        flag = True
        for i in range(0, 3):
            for j in range(0, 3):
                if j + 1 < 3 and i + 1 < 3:
                    if (self.board[i][j] != self.board[i + 1][j + 1]):
                        flag = False
                        break
        return flag


ackd = checkerboard()
print(ackd.board)
ackd.fill('O', 0, 0)
ackd.fill('X', 0, 1)
ackd.fill('X', 0, 2)
ackd.fill('O', 1, 0)
ackd.fill('X', 1, 1)
ackd.fill('X', 1, 2)
ackd.fill('O', 2, 0)
ackd.fill('X', 2, 1)
ackd.fill('X', 2, 2)
